package com.mc.web.controller.gpm;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import com.mc.clt.domain.CltResultGermplasm;
import com.mc.clt.service.ICltService;
import com.mc.common.core.domain.entity.SysUser;
import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.framework.web.service.TokenService;
import com.mc.gpm.domain.GpmShelve;
import com.mc.gpm.domain.GpmStorage;
import com.mc.gpm.service.IGpmShelveService;
import com.mc.lab.domain.LabAuthenticate;
import com.mc.lab.service.ILabAuthenticateService;
import com.mc.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mc.common.annotation.Log;
import com.mc.common.core.controller.BaseController;
import com.mc.common.core.domain.AjaxResult;
import com.mc.common.enums.BusinessType;
import com.mc.gpm.domain.GpmInStorage;
import com.mc.gpm.service.IGpmInStorageService;
import com.mc.common.utils.poi.ExcelUtil;
import com.mc.common.core.page.TableDataInfo;

/**
 * 种质资源入库Controller
 *
 * @author mc
 * @date 2021-03-19
 */
@RestController
@RequestMapping("/gpm/inStorage")
public class GpmInStorageController extends BaseController
{
    @Autowired
    private IGpmInStorageService gpmInStorageService;
    @Autowired
    private ICltService cltService;
    @Autowired
    private ILabAuthenticateService authenticateService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IGpmShelveService shelveService;

    /**
     * 查询种质资源入库列表
     */
    @PreAuthorize("@ss.hasPermi('gpm:storage:list')")
    @GetMapping("/list")
    public TableDataInfo list(GpmInStorage gpmInStorage)
    {
        startPage();
        List<GpmInStorage> list = gpmInStorageService.selectGpmInStorageList(gpmInStorage);
        return getDataTable(list);
    }

    /**
     * 导出种质资源入库列表
     */
    @PreAuthorize("@ss.hasPermi('gpm:storage:export')")
    @Log(title = "种质资源入库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(@RequestBody GpmInStorage gpmInStorage)
    {
        List<GpmInStorage> list = gpmInStorageService.selectGpmInStorageList(gpmInStorage);
        ExcelUtil<GpmInStorage> util = new ExcelUtil<GpmInStorage>(GpmInStorage.class);
        return util.exportExcel(list, "storage");
    }

    /**
     * 获取种质资源入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('gpm:storage:query')")
    @GetMapping(value = "/{dtorageId}")
    public AjaxResult getInfo(@PathVariable("dtorageId") String dtorageId)
    {
        return AjaxResult.success(gpmInStorageService.selectGpmInStorageById(dtorageId));
    }

    /**
     * 种质资源入库
     */
    @PreAuthorize("@ss.hasPermi('gpm:storage:add')")
    @Log(title = "种质资源入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GpmInStorage gpmInStorage)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser sysUser = userService.selectUserByUserName(loginUser.getUsername());
        List <CltResultGermplasm> list = cltService.selectGermplasmByCltID(gpmInStorage.getCltId());
        CltResultGermplasm cltResultGermplasm = list.get(0);
        LabAuthenticate labAuthenticate = authenticateService.selectLabAuthenticateByCltID(gpmInStorage.getCltId());
        GpmShelve gpmShelve = shelveService.selectGpmShelveById(gpmInStorage.getShelveId());
        GpmStorage gpmStorage = new GpmStorage();
        String uuid  = UUID.randomUUID().toString().replaceAll("-","");
        gpmStorage.setStorageId(uuid);
        gpmStorage.setCltId(gpmInStorage.getCltId());
        gpmStorage.setTaskId(cltResultGermplasm.getTaskId());
        gpmStorage.setGermplasmCode(cltResultGermplasm.getGermplasmId());
        gpmStorage.setShelveId(gpmInStorage.getShelveId());
        gpmStorage.setInActive(gpmInStorage.getInActive());
        gpmStorage.setAmount(gpmInStorage.getInAmount());
        gpmStorage.setInUserId(gpmInStorage.getInUserId());
        gpmStorage.setGrain(gpmInStorage.getInGrain());
        gpmStorage.setPurity(gpmInStorage.getInPurity());
        gpmStorage.setName(labAuthenticate.getName());
        gpmStorage.setSpecies(labAuthenticate.getScientificName());
        gpmStorage.setFamily(labAuthenticate.getFamilyName());
        gpmStorage.setGenus(labAuthenticate.getCategoryName());
        gpmStorage.setLocation(cltResultGermplasm.getLocation());
        gpmStorage.setLatitude(cltResultGermplasm.getLatitude());
        gpmStorage.setElecation(cltResultGermplasm.getElecation());
        gpmStorage.setSlopeDegree(cltResultGermplasm.getSlopeDegree());
        gpmStorage.setSlopeAcpect(cltResultGermplasm.getSlopeAcpect());
        gpmStorage.setSlopePosition(cltResultGermplasm.getSlopePosition());
        gpmStorage.setAssociateSpecies(cltResultGermplasm.getAssociateSpecies());
        gpmStorage.setTopography(cltResultGermplasm.getTopography());
        gpmStorage.setCltTime(cltResultGermplasm.getCollectionTime());
        gpmStorage.setLocation(cltResultGermplasm.getLocation());
        gpmStorage.setCltUserId(cltResultGermplasm.getNickName());
        gpmStorage.setStatus("0");//0代表有效
        gpmStorage.setCreateTime(DateUtils.getNowDate());
        gpmStorage.setCreateUserId(loginUser.getUsername());
        long day = gpmInStorage.getActiveDate().multiply(new BigDecimal(365)).setScale(0,BigDecimal.ROUND_UP).longValue() ;//向上取整
        day = day*24*60*60*1000; // 要加上的天数转换成毫秒数
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 日期格式
        long nowDate = DateUtils.getNowDate().getTime();
        nowDate += day; //
        gpmStorage.setActiveExprieDate(new Date(nowDate));
        gpmInStorage.setCreateBy(loginUser.getUsername());
        gpmInStorage.setCreateTime(DateUtils.getNowDate());
        gpmStorage.setInUserName(sysUser.getNickName());
        gpmStorage.setShelveNumber(gpmShelve.getNumber());
        //gpmStorage.setCltUserName();
        gpmStorage.setOutCount(0);
        //gpmStorage.set
        return toAjax(gpmInStorageService.insertGpmInStorage(gpmInStorage,gpmStorage));
    }

    /**
     * 修改种质资源入库
     */
    @PreAuthorize("@ss.hasPermi('gpm:storage:edit')")
    @Log(title = "种质资源入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GpmInStorage gpmInStorage)
    {
        return toAjax(gpmInStorageService.updateGpmInStorage(gpmInStorage));
    }


}